#include "SDL2/SDL.h"
#include <iostream>

//定义屏幕尺寸常量
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;

using namespace std;

SDL_Window *window = nullptr;
SDL_Renderer *renderer = nullptr;

SDL_Texture* LoadImage(string file)
{
    SDL_Surface *loadedImage = nullptr;
    SDL_Texture *texture = nullptr;
  
    loadedImage = SDL_LoadBMP(file.c_str());
    if (loadedImage != nullptr)
    {
        texture = SDL_CreateTextureFromSurface(renderer, loadedImage);
        SDL_FreeSurface(loadedImage);
    }
    else
    {
        cout << SDL_GetError() << endl;
    }
    return texture;
}


void ApplySurface(int x, int y, SDL_Texture *tex, SDL_Renderer *rend)
{
    SDL_Rect pos;
    pos.x = x;
    pos.y = y;
    SDL_QueryTexture(tex, NULL, NULL, &pos.w, &pos.h);
 
    SDL_RenderCopy(rend, tex, NULL, &pos);
}

int main(int argc, char** argv)
{
    (void)argc;
    (void)argv;
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {
        cout << SDL_GetError() << endl;
        return 1;
    }
 
    window = SDL_CreateWindow("Lesson 2", SDL_WINDOWPOS_CENTERED, 
        SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
    if (window == nullptr)
    {
        cout << SDL_GetError() << endl;
        return 2;
    }

    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED 
        | SDL_RENDERER_PRESENTVSYNC);
    if (renderer == nullptr)
    {
        cout << SDL_GetError() << endl;
        return 3;
    }

    SDL_Texture *background = nullptr;
    SDL_Texture *image = nullptr;
    background = LoadImage("../assert/background.bmp");
    image = LoadImage("../assert/actor.bmp");
    if (background == nullptr || image == nullptr)
    {
        return 4;
    }

    SDL_RenderClear(renderer);

    int bW, bH;
    SDL_QueryTexture(background, NULL, NULL, &bW, &bH);
    ApplySurface(0, 0, background, renderer);
    ApplySurface(bW, 0, background, renderer);
    ApplySurface(0, bH, background, renderer);
    ApplySurface(bW, bH, background, renderer);

    int iW, iH;
    SDL_QueryTexture(image, NULL, NULL, &iW, &iH);
    int x = WINDOW_WIDTH / 2 - iW / 2;
    int y = WINDOW_HEIGHT / 2 - iH / 2;
    ApplySurface(x, y, image, renderer);

    SDL_RenderPresent(renderer);
    SDL_Delay(2000);

    SDL_DestroyTexture(background);
    SDL_DestroyTexture(image);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
 
    SDL_Quit();
  
    return 0;
}